package com.spboot.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.spboot.entity.Address;
import com.spboot.entity.Shoppingcart;
import com.spboot.entity.User;
import com.spboot.service.AddressService;
import com.spboot.service.ShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-04-27
 */
@Controller
@RequestMapping("/shoppingcart")
public class ShoppingcartController {

    @Autowired
    ShoppingcartService shoppingcartService;

    @Autowired
    AddressService addressService;

    /**
     * 商品id
     */
    public static String carthao;

    /**
     * 商品数量
     */
    public static String shuliang;


    /**
     * 查询用户购物车
     * @param model
     * @param userid
     * @return
     */
    @RequestMapping("select")
    public String select(Model model,Integer userid){
        model.addAttribute("select",shoppingcartService.selectall(userid));
        model.addAttribute("shu",shoppingcartService.ssum(userid));
        model.addAttribute("zong",shoppingcartService.zong(userid));
        String zong=shoppingcartService.zong(userid).toString();
        model.addAttribute("zonga",zong);
        return "shopping";
    }

    /**
     * 删除购物车
     * @param cartid
     * @param userid
     * @return
     */
    @RequestMapping("deletecart")
    public  String delete(Long cartid,Integer userid){
        Long delete = shoppingcartService.deletecart(cartid,userid);
        if(delete==1){
            return "select";
        }
        return "select";
    }

    /**
     * 添加购物车
     * @param informationid 商品id
     * @param editionid 版本id
     * @param colorid 颜色id
     * @param priceid 价格id
     * @param userid 用户id
     * @param model model
     * @return
     */
    @RequestMapping("insert")
    public String insert(Long informationid,Long editionid,Long colorid,Long priceid,Long userid,Model model){
        //1.先查询数据库是否有这个颜色版本价格
        Integer sum=shoppingcartService.selectsum(informationid, editionid, colorid, priceid);
        if (sum == 1){
            //2.如果有查询tableid
            Integer id=shoppingcartService.selectid(informationid, editionid, colorid, priceid);
            //3.添加购物车
            int insert=shoppingcartService.insertcart(informationid, userid, id);

        }else{
            //2.如果数据库没有这个颜色版本价格添加一个
            int tableid=shoppingcartService.inserttable(informationid, editionid, colorid, priceid);
            //3.查询tableid
            Integer id=shoppingcartService.selectid(informationid, editionid, colorid, priceid);
            //4.添加购物车
            int insert=shoppingcartService.insertcart(informationid, userid, id);
        }
        return "redirect:/information/purchase?informationid="+informationid;
    }

    /**
     * 接收商品购买选中的商品id
     * @param cartid
     * @return
     */
    @RequestMapping("ajax")
    @ResponseBody
    public String ceshi(String cartid,String number){
        carthao =cartid ;
        shuliang = number;
        return cartid+shuliang;
    }

    /**
     * 商品购买页2
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("ajaxb")
    public String ceshib(Model model, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("login");
        //地址
        model.addAttribute("address",addressService.saddress(user.Userid));
        String []num = shuliang.split(",");//把获取到的单个商品数量数组分开
        int []shu = new int[num.length];

        String []cart = carthao.split(",");//把获取到的cartid数组分开
        int []cartb = new int[cart.length];

        String []sumprice = new String[cart.length];//单个商品总价
        Integer sumpriceb = 0;//全部商品总价
        String zongjia = null;
        Integer price;
        Integer sum;
        Integer number=cart.length;//商品件数
        List<Shoppingcart> cartlist = new ArrayList<Shoppingcart>();
        for (int i = 0;i < cart.length;i++){
            cartb[i]= Integer.parseInt(cart[i]);//每个选中商品的id
            shu[i]= Integer.parseInt(num[i]);//每个选中商品的数量
            //修改数据库数量
            shoppingcartService.updatecart(shu[i],(long) cartb[i]);
            Shoppingcart shoppingcart = new Shoppingcart();
            shoppingcart=shoppingcartService.selectcart((long) cartb[i]);
            price=Integer.parseInt(shoppingcart.getPricea());//单个商品价格
            sum=shoppingcart.getNumber();//单个商品数量
            sumprice[i]= String.valueOf(price*sum);//单个商品总价
            shoppingcart.setTotalprice(sumprice[i]);////单个商品总价
            sumpriceb+=Integer.parseInt(sumprice[i]);//商品总价
            zongjia=sumpriceb.toString();
            cartlist.add(shoppingcart);
        }
        model.addAttribute("number",number);
        model.addAttribute("sumprice",zongjia);
        model.addAttribute("cartlist",cartlist);
        return "cartready";
    }


    /**
     * 订单添加
     * @param request
     * @return
     */
    @RequestMapping("/orderadd")
    public String add(HttpServletRequest request) {
        //存放座位id
        String dingdan = (String) request.getSession().getAttribute("dingdan");
        //System.out.println(dingdan+"---------");
        return "ce";
    }

    /**
     * 显示地址
     * @param addressid
     * @return
     */
    @RequestMapping("updateajax")
    @ResponseBody
    public Address updateajax(Long addressid){
        Address address = new Address();
        address = addressService.getById(addressid);
        return address;
    }

    /**
     * 购买页修改用户地址
     * @param contacts
     * @param phone
     * @param address
     * @param addressid
     * @param userid
     * @return
     */
    @RequestMapping("updateaddress")
    public String update(String contacts, String phone, String address, Integer addressid,Integer userid){
        int shifou = addressService.updateaddress(contacts, phone, address, addressid);
        if(shifou!=0){
            return "ajaxb?userid="+userid;
        }
        return "";
    }

    /**
     * 购买页添加用户地址
     * @param address
     * @param userid
     * @param contacts
     * @param phone
     * @return
     */
    @RequestMapping("insertaddress")
    public String insert(String address,Integer userid,String contacts,String phone){
        int shifou = addressService.insertaddress(address, userid, contacts, phone);
        if(shifou!=0){
            return "ajaxb?userid="+userid;
        }
        return "";
    }
}

